package com.xiaoyu.client.ui.fragment.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.xiaoyu.client.R;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.forum.ForumAddActivity;
import com.xiaoyu.client.ui.fragment.forum.ForumFragment;
import com.xiaoyu.client.ui.fragment.forum.ForumHighFragment;
import com.xiaoyu.client.ui.fragment.forum.ForumImgFragment;
import com.xiaoyu.client.ui.fragment.forum.ForumLatelyFragment;
import com.xiaoyu.client.ui.fragment.forum.ForumTitleFragment;
import com.xiaoyu.client.ui.fragment.forum.ForumVideoFragment;
import com.xiaoyu.commonlib.utils.CommonFragmentPagerAdapter;
import com.xiaoyu.commonlib.utils.ViewPagerUtils;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCUtils;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * 亿友圈
 * Created by jk on 2018/9/28.
 */
public class BillionCircleFragment extends Fragment implements View.OnClickListener{
    private View mView;
    private LinearLayout functionBar;
    private ImageView publishBtn;//发布按钮
    private View mLine;
    private ViewPager mViewPager;
    private ViewPagerUtils mPagerUtils;
    private String[] categorys = new String[]{"high","lately","video","title","img"};
    private List<Fragment> mList = new ArrayList<>();
    private CommonFragmentPagerAdapter mPageAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (mView == null){
            mView = inflater.inflate(R.layout.fragment_billion_circle,container,false);
            initView();
        }
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null){
            parent.removeView(mView);
        }
        return mView;
    }



    //初始化控件
    private void initView() {
        functionBar = mView.findViewById(R.id.fragment_billion_circle_function_bar);
        mLine = mView.findViewById(R.id.fragment_billion_circle_function_bar_line);
        mViewPager = mView.findViewById(R.id.fragment_billion_circle_view_pager);
        publishBtn = mView.findViewById(R.id.fragment_billion_circle_publish_btn);

        initPager();

        //上拉下拉刷新设置
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsHeader(context);
            }
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });

        mViewPager.addOnPageChangeListener(pageChangeListener);
        for (int i =0;i< functionBar.getChildCount();i++){
            functionBar.getChildAt(i).setOnClickListener(this);
        }
        publishBtn.setOnClickListener(this);
    }

    private void initPager() {
        mPagerUtils = new ViewPagerUtils(getActivity(),functionBar,mLine);
        mPagerUtils.initLine(45);
        mPagerUtils.setTextColor("#333333","#0c93e9");
        mPagerUtils.selectItem(0);

        mList.clear();
        mList.add(0,new ForumHighFragment());
        mList.add(1,new ForumLatelyFragment());
        mList.add(2,new ForumVideoFragment());
        mList.add(3,new ForumTitleFragment());
        mList.add(4,new ForumImgFragment());

        mPageAdapter = new CommonFragmentPagerAdapter(getFragmentManager(),mList);
        mViewPager.setAdapter(mPageAdapter);
        mViewPager.setCurrentItem(0);
    }


    @Override
    public void onClick(View v) {
        if (v == functionBar.getChildAt(0)){//精选

            mPagerUtils.selectItem(0);
            mPagerUtils.scrollLineMove(0,0);
            mViewPager.setCurrentItem(0);

        }else if (v == functionBar.getChildAt(1)){//最新

            mPagerUtils.selectItem(1);
            mPagerUtils.scrollLineMove(1,0);
            mViewPager.setCurrentItem(1);

        }else if (v == functionBar.getChildAt(2)){//视频

            mPagerUtils.selectItem(2);
            mPagerUtils.scrollLineMove(2,0);
            mViewPager.setCurrentItem(2);

        }else if (v == functionBar.getChildAt(3)){//文字

            mPagerUtils.selectItem(3);
            mPagerUtils.scrollLineMove(3,0);
            mViewPager.setCurrentItem(3);

        }else if (v == functionBar.getChildAt(4)){//图片

            mPagerUtils.selectItem(4);
            mPagerUtils.scrollLineMove(4,0);
            mViewPager.setCurrentItem(4);

        }else if (v == publishBtn){//发布
            Intent intent = new Intent(getActivity(), ForumAddActivity.class);
            startActivity(intent);

        }
    }


    /**
     * viewPager切换监听
     */
    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            mPagerUtils.scrollLineMove(position,positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            mPagerUtils.selectItem(position);
            mViewPager.setCurrentItem(position);
            JCVideoPlayer.releaseAllVideos();
            JCUtils.getAppCompActivity(getActivity()).getSupportActionBar().hide();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };


    @Override
    public void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
        JCUtils.getAppCompActivity(getActivity()).getSupportActionBar().hide();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                getActivity().finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}
